<?php
namespace App\Logics;

use App\Models\ChannelCateGoodsModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\HomePageModel;
use App\Models\GoodsModel;
use App\Models\SystermAdModel;
use App\Models\ChannelModel;
use App\Models\CategoryModel;

//逻辑处理
class HomePageLogic extends BaseLogic {


    public function __construct()
    {
        parent::__construct();

    }

    public function index(array $para){
        $cha_id = isset($para['cha_id'])? (int)$para['cha_id']:0;
        $cate_id=isset($para['cate_id'])?(int)$para['cate_id']:1;

        $channel=ChannelModel::where('cha_id',$cha_id)->select('cha_name','cha_describe','cha_id')->first();
        $channel=ob_to_array($channel);
        if(empty($channel)){
            return '参数有误';
        }

        $category=CategoryModel::where([['cha_id',$cha_id],['pid',0]])->select('cate_id','cate_name')->get();
        $category=ob_to_array($category);
        $newGoods=$this->getNewGoods($cha_id,$cate_id);

        $selectGoods=$this->getSelectGoods($cha_id,$cate_id);
        $banner=[];
        if($cate_id>1){
            $banner=$this->getBanner($cha_id,$cate_id);
        }

        $url=config('sys.img_url');
        return [
            'category'=>$category,
            'newGoods'=>$newGoods,
            'channel'=>$channel,
            'selectGoods'=>$selectGoods,
            'banner'=>$banner,
            'cate_id'=>$cate_id,
            'cha_id'=>$cha_id,
            'url'=>$url
        ];
    }




    public function addNewGoods(array $para){
        $goods_id=isset($para['goods_id'])?(int)$para['goods_id']:0;
        $cha_id=isset($para['cha_id'])?(int)$para['cha_id']:0;
        $cate_id=isset($para['cate_id'])?(int)$para['cate_id']:0;

        if($goods_id<1){
            return error('请选择商品');
        }
        if($cha_id<1){
            return error('缺少参数');
        }
        $goods=GoodsModel::where('goods_id',$goods_id)->select('is_putaway')->first();
        if(empty($goods)){
            return error('该商品不存在');
        }
        $goods=ob_to_array($goods);
        if($goods['is_putaway'] !=3){
            return error('该商品不是上架商品');
        }

        $channel=ChannelModel::where('cha_id',$cha_id)->count();
        if(empty($channel)){
            return error('该渠道不存在');
        }

        $res=HomePageModel::where([['cha_id',$cha_id],['cate_id',$cate_id],['goods_id',$goods_id]])->count();
        if($res){
            return error('该商品已经添加过');
        }
        $data=[
            'cate_id'=>$cate_id,
            'cha_id'=>$cha_id,
            'goods_id'=>$goods_id,
            'sort'=>5
        ];
        $res=HomePageModel::insert($data);
        if($res){
            return success('添加成功');
        }
        return error('添加失败');
    }


    /** 新品推荐商品
     * @param int $cha_id
     * @return mixed
     */
    public function getNewGoods(int $cha_id,int $cate_id){

        $cate_id=$cate_id>1?$cate_id:1;
        $fields="goods_id,goods_name,goods_official_price,goods_price,goods_img_list,goods_tenancy_term,keyword";

        $home_goods=HomePageModel::where([['cha_id',$cha_id],['cate_id',$cate_id]])->orderBy('sort','asc')
            ->select('goods_id','sort','id')->get();
        $home_goods=ob_to_array($home_goods);
        if(empty($home_goods)) return false;

        $goods_ids=array_column($home_goods,'goods_id');
        $goods=GoodsModel::whereIn('goods_id',$goods_ids)->where([['is_putaway',3],['is_delete',0]])->selectRaw($fields)->get();
        $goods_ar=[];
        foreach($goods as $k=>$v){
            $goods_ar[$v['goods_id']]=$v;
        }
        $ids=[];
        foreach($home_goods as $k=>$v){
            if(isset($goods_ar[$v['goods_id']])){
                $home_goods[$k]['goods_img_list']=isset($goods_ar[$v['goods_id']])?$goods_ar[$v['goods_id']]['goods_img_list']:'';
                $home_goods[$k]['goods_name']=isset($goods_ar[$v['goods_id']])?$goods_ar[$v['goods_id']]['goods_name']:'';
            }else{
                $ids[]=$v['id'];
                unset($home_goods[$k]);
            }
        }

        if($ids){
            HomePageModel::whereIn('id',$ids)->delete();
        }

        return ob_to_array($home_goods);
    }


    /** 获取分类banner
     * @param int $cha_id
     * @param int $cate_id
     * @return mixed
     */
    public function getBanner(int $cha_id,int $cate_id){
        $data=SystermAdModel::where([['cha_id',$cha_id],['cate_id',$cate_id]])->first();

        return ob_to_array($data);
    }


    /** 上传排版中的新品推荐
     * @param int $id
     * @return mixed
     */
    public function newGoodsDelete(int $id){
        $res=HomePageModel::where('id',$id)->delete();

        if($res){
            return success('删除成功');
        }
        return error('删除失败');
    }

    /**
     * 获取可搜索的商品
     * @param int $cha_id
     * @return bool
     */
    public function getSelectGoods(int $cha_id,int $cate_id){
        $cate_id=$cate_id>1?$cate_id:1;
        $goods_ids=ChannelCateGoodsModel::where('cha_id',$cha_id)->pluck('goods_id');
        $goods_ids=ob_to_array($goods_ids);

        if(empty($goods_ids)) return false;

        $ids=HomePageModel::where([['cate_id',$cate_id],['cha_id',$cha_id]])->pluck('goods_id');
        $ids=ob_to_array($ids);
        $goods_ids=array_diff($goods_ids,$ids);
        $goods=GoodsModel::whereIn('goods_id',$goods_ids)->where([['is_putaway',3],['is_delete',0]])->select('goods_id','goods_name')->get();
        return ob_to_array($goods);
    }

    /** banner图片处理
     * @param Request $request
     * @param $para
     * @return mixed
     */
    public function bannerImg(Request $request,$para){
        $id=isset($para['id'])?(int)$para['id']:0;
        $cha_id=isset($para['cha_id'])?(int)$para['cha_id']:0;
        $cate_id=isset($para['cate_id'])?(int)$para['cate_id']:0;

        if(!$request->hasFile('img')){
            return error('请上传图片');
        }

        $channel=ChannelModel::where('cha_id',$cha_id)->select('cha_name')->first();
        if(empty($channel)){
            return error('参数有误');
        }
        $channel=ob_to_array($channel);
        if($cate_id>1){
            $res=CategoryModel::where([['cha_id',$cha_id],['cate_id',$cate_id]])->count();
            if(empty($res)){
                return error('参数有误');
            }
        }

        $img='';
        if($id>0) {
            $ad = SystermAdModel::where([['id', $id], ['cha_id', $cha_id], ['cate_id', $cate_id]])->select('litpic')->first();
            if (empty($ad)) return error('参数有误');
            $ad = ob_to_array($ad);
            $img=$ad['litpic'];
        }
            $new_img='';
            $data=$this->uploadImg($request,'img','banner');
            if($data['status']==0) return error($data['data']);
            $img=$data['data'];
            $new_img=$data['data'];

            $data=[
                'litpic'=>$img,
                'cha_id'=>$cha_id,
                'cate_id'=>$cate_id,
                'show_location'=>$channel['cha_name'],
                'is_show_share'=>1
            ];
            if($id>0){
                $res=SystermAdModel::where('id',$id)->update($data);
                if($res){
                    if($ad['litpic'] && $new_img) $this->deleteImg($ad['litpic']);
                    return success('修改成功');
                }
                if($new_img) $this->deleteImg($new_img);
                return error('修改失败');
            }

            $data['create_time']=time();
            $res=SystermAdModel::insert($data);

            if($res){
                return success('添加成功');
            }
            if($new_img) $this->deleteImg($new_img);
            return error('添加失败');
    }

    public function bannerDelete(array $para){
        $id=isset($para['id'])?(int)$para['id']:0;
        $cha_id=isset($para['cha_id'])?(int)$para['cha_id']:0;
        $cate_id=isset($para['cate_id'])?(int)$para['cate_id']:0;
        $res=SystermAdModel::where([['id',$id],['cha_id',$cha_id],['cate_id',$cate_id]])->delete();
        if($res){
            return success('删除成功');
        }
        return error('删除失败');
    }

    /** 异步修改banner url
     * @param array $para
     * @return mixed
     */
    public function ajaxEditBannerUrl(array $para){
        $id=isset($para['id'])?(int)$para['id']:0;
        $url=isset($para['url'])?$para['url']:'';

        if($id<1 ){
            return error('参数有误');
        }

        if(empty($url)){
            return error('跳转链接不能为空');
        }

        $res=SystermAdModel::where('id',$id)->update(['content_url'=>$url]);
        if($res){
            return success('修改成功');
        }
        return error('修改失败');
    }

    /**
     * @param array $para
     * @return mixed
     */
    public function ajaxEditSort(array $para){
        $id=isset($para['id'])?(int)$para['id']:0;
        $sort=isset($para['sort'])?(int)$para['sort']:'';
        if($id<1 ){
            return error('参数有误');
        }
        $res=HomePageModel::where('id',$id)->update(['sort'=>$sort]);
        if($res){
            return success('修改成功');
        }
        return error('修改失败');

    }









}
